import 'package:flutter/material.dart';
import 'navbar/navbar.dart';
import 'package:dio/dio.dart';
import 'package:shared_preferences/shared_preferences.dart';

Dio dio = new Dio();

class PoliticalTheory extends StatefulWidget {
  PoliticalTheory({Key key}) : super(key: key);

  _PoliticalTheoryState createState() => _PoliticalTheoryState();
}

class _PoliticalTheoryState extends State<PoliticalTheory> {
  // var _res;
  // void message() async {
  //   Response response = await dio.get('http://xssw.gdit.edu.cn/sizheng/api/v1/student/5ddcd624f1a2213b08c8da51');
  //   _res = response.data;
  //   setState(() {});
  // }
  var user_id;
  var xuehao;
  void getName() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    user_id = sharedPreferences.getString('user_id');
    xuehao = sharedPreferences.getString('number');
    denglvzt();
    setState(() {});
  }

  void denglvzt() async {
    Response response;
    if (xuehao.length < 10) {
      response = await dio
          .get('http://xssw.gdit.edu.cn/sizheng/api/v1/teacher/${user_id}');
          var point = response.data['point'];
      if (response.data['mission']['login'] != 'true') {
        await dio.patch(
            'http://xssw.gdit.edu.cn/sizheng/api/v1/teacher/${user_id}',
            data: {
              "mission": {
                "login": "true",
              },
              "point":point+1,
            });
      }
    } else {
      response = await dio
          .get('http://xssw.gdit.edu.cn/sizheng/api/v1/student/${user_id}');
          var point = response.data['point'];
      if (response.data['mission']['login'] != 'true') {
        await dio.patch(
            'http://xssw.gdit.edu.cn/sizheng/api/v1/student/${user_id}',
            data: {
              "mission": {
                "login": "true",
              },
              "point":point+1,
            });
      }
    }
  }

  @override
  void initState() {
    super.initState();
    getName();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Image.asset(
          'images/title.jpg',
          height: 25,
        ),
        actions: <Widget>[
          //     Container(
          //       child: InkWell(
          //         child: Row(
          //           children: <Widget>[
          //             Container(
          //               child: Text(
          //                 '积分',
          //                 style: TextStyle(
          //                   fontSize: 10.0,
          //                   fontWeight: FontWeight.w900,
          //                 ),
          //               ),
          //               width: 15,
          //               alignment: Alignment.center,
          //             ),
          //             _res==null
          //             ? Container(
          //               margin: EdgeInsets.only(right: 5.0),
          //               child: Text('0',
          //               style: TextStyle(
          //                   fontSize: 20.0, fontWeight: FontWeight.w600)),
          //           alignment: Alignment.center,
          //         )
          //             :Container(
          //               margin: EdgeInsets.only(right: 5.0),
          //               child: Text('${_res['point']}',
          //               style: TextStyle(
          //                   fontSize: 20.0, fontWeight: FontWeight.w600)),
          //           alignment: Alignment.center,
          //         ),
          //       ],
          //     ),
          //     onTap: () {
          //       Navigator.of(context)
          //           .push(MaterialPageRoute(builder: (context) => Jifen()));
          //     },
          //   ),
          // ),
          // Container(
          //   child: Text('|',
          //       style: TextStyle(
          //         fontSize: 25.0,
          //         fontWeight: FontWeight.w100,
          //         color: Colors.red[200],
          //       )),
          //   alignment: Alignment.center,
          //   width: 10.0,
          // ),
          // Container(
          //     child: InkWell(
          //   child: Container(
          //     width: 100.0,
          //     margin: EdgeInsets.fromLTRB(0, 13, 5, 13),
          //     decoration: BoxDecoration(
          //         borderRadius: BorderRadius.circular(15),
          //         border: Border.all(width: 1, color: Colors.red[200])),
          //     child: Row(

          //       children: <Widget>[
          //         Container(
          //           child: Text('我爱学习'),
          //           margin: EdgeInsets.fromLTRB(10, 0, 0, 0),
          //         ),
          //         Container(
          //           child: Icon(Icons.search),
          //           margin: EdgeInsets.fromLTRB(5, 0, 0, 0),
          //         ),
          //       ],
          //     ),
          //   ),
          //   onTap: (){
          //     Navigator.of(context).push(
          //               MaterialPageRoute(builder: (context) => SearchPage()));
          //   },
          // )),
          // Container(
          //   margin: EdgeInsets.fromLTRB(0, 0, 3, 0),
          //     width: 35.0,
          //     height: 35.0,
          //     decoration: ShapeDecoration(
          //         shape: CircleBorder(),
          //         image: DecorationImage(
          //             fit: BoxFit.cover,
          //             image: AssetImage("images/head.jpg",))))
        ],
      ),
      body: NavBar(),
    );
  }
}
